define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'moment'], function ($, undefined, Backend, Table, Form, Moment) {

    var Controller = {
        _queryString: '',
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zzyy/room/index' + Controller._queryString,
                    add_url: 'zzyy/room/add' + Controller._queryString,
                    edit_url: 'zzyy/room/edit',
                    del_url: 'zzyy/room/del',
                    multi_url: 'zzyy/room/multi',
                    import_url: 'zzyy/room/import',
                    table: 'zzyy_room',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='store_id']", form).addClass("selectpage")
                    .data("source", "zzyy/store/index")
                    .data("primaryKey", "id").data("field", "store_name");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'store_id', title: __('Store_id'), operate: '=', visible: false},
                        {field: 'store.store_name', title: __('Store_id'), operate: false},
                        {field: 'room_name', title: __('Room_name'), operate: 'LIKE'},
                        {field: 'room_bannerimage', title: __('Room_bannerimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'room_area', title: __('Room_area'), operate: false},
                        {field: 'room_personnum', title: __('Room_personnum'), operate: false},
                        {field: 'wifi_name', title: __('Wifi_name'), operate: false},
                        {field: 'bluetooth_audio_name', title: __('Bluetooth_audio_name'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), visible: false, operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'zy_button',
                                    text: '设置租用时间',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-check-square',
                                    url: function (row) {
                                        return Fast.api.fixurl('zzyy/room/renttime?id=' + row.id)
                                    },
                                    extend: "data-area='[\"60%\", \"80%\"]'",
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ],
                searchFormVisible: true,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        renttime: function () {
            Controller.api.bindevent();

            $('#ggg').click(function (){
                let starttime = $('#starttime').val();
                let endtime = $('#endtime').val();
                let interval = $('#pill_duration').val();
                let price = $('#pill_price').val();

                var startArray = starttime.split(":");
                var startHour = parseInt(startArray[0], 10);
                var startMin = parseInt(startArray[1], 10);

                var endArray = endtime.split(":");
                var endHour = parseInt(endArray[0], 10);
                var endMin = parseInt(endArray[1], 10);

                var curTime = Moment({hour: startHour, minute: startMin})
                var endTime = Moment({hour: endHour, minute: endMin})

                var pills = [];
                var pillEndTime = Moment({hour: curTime.hour(), minute: curTime.minute()}).add('m', interval - 1)
                while (pillEndTime.isBefore(endTime)) {
                    pills.push({
                        'start': curTime.format('HH:mm'),
                        'end': pillEndTime.format('HH:mm'),
                        'price': price
                    })
                    curTime.add('m', interval)
                    pillEndTime = Moment({hour: curTime.hour(), minute: curTime.minute()}).add('m', interval - 1)
                }

                if (curTime.isBefore(endTime)) {
                    pills.push({
                        'start': curTime.format('HH:mm'),
                        'end': endTime.format('HH:mm'),
                        'price': price
                    })
                }

                console.log(JSON.stringify(pills))

                $('#ccc').text(JSON.stringify(pills));
                Controller.api.bindevent();
            });

            Form.api.bindevent($("div.g_tool_container"));

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            queryString: function () {
                return location.search.replace("dialog=1", "").split('&').filter(function (item) {
                    return !!item;
                }).join("&");
            }
        }
    };
    Controller._queryString = Controller.api.queryString();
    return Controller;
});